package ahblue.cn.tanzi.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.daimajia.numberprogressbar.NumberProgressBar;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.model.VersionInfo;


/**
 * @author ffc
 * @version 创建时间：2015-11-16 上午11:36:12
 */
public class UpdateProgressbarWindow extends PopupWindow {

    private View mMenuView;
    private Activity context;
    private TextView tv_name;
    private NumberProgressBar progressBar;
    private VersionInfo version;

    public static final Uri CONTENT_URI = Uri.parse("content://downloads/dpy_downloads");
    public UpdateProgressbarWindow(Activity context, VersionInfo version) {
        super(context);
        this.context = context;
        this.version = version;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.poppupwindow_update, null);

        tv_name = (TextView) mMenuView.findViewById(R.id.pop_update_name);
        progressBar = (NumberProgressBar) mMenuView.findViewById(R.id.pop_update_probar);
        tv_name.setText("藏宝鼠"+version.getVersion());
        //设置按钮监听
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(context.getWindowManager().getDefaultDisplay().getWidth() * 6 / 10);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(context.getWindowManager().getDefaultDisplay().getWidth() * 2 / 5);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setBackgroundDrawable(new ColorDrawable(0));
//		//设置SelectPicPopupWindow弹出窗体动画效果
//		this.setAnimationStyle(R.style.downToUp_anim);
//        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable cd = new ColorDrawable(0xeeffffff);
//        //设置SelectPicPopupWindow弹出窗体的背景
//        this.setBackgroundDrawable(cd);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1f);

            }
        });
        setBackgroundAlpha(0.5f);


    }

    /**
     * 设置背景透明度
     *
     * @param bgAlpha
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        context.getWindow().setAttributes(lp);
        if(bgAlpha==1){

        }
    }

    @Override
    public void dismiss() {
        if (context != null && !context.isFinishing()){
            super.dismiss();
        }
    }

    public NumberProgressBar getProgressBar() {
        return progressBar;
    }

    public void setProgressBar(NumberProgressBar progressBar) {
        this.progressBar = progressBar;
    }
}